﻿#include "WRWorkGuiApplication.h"
#include "WRWorkMainWindow.h"
#include "Runtime/WRWorkGuiConfig.h"
#include "../WRSoft-Utility/WRObjRef.h"
#include "../WRSoft-Utility/Runtime/WRAppContext.h"
#include "../WRSoft-Working-Common/WRWorkDaemon.h"

#include <QMessageBox>
#include <QLocalSocket>
#include <QLocalServer>

//初始化当前对象。
WRWorkGuiApplication::WRWorkGuiApplication(int &argc, char **argv, const QString& configFileName)
    : m_app(argc, argv)
{
    m_window = 0;
    m_server = new QLocalServer(this);
    //设置程序默认字体大小。
    QFont font = m_app.font();
    font.setPointSize(9);
    m_app.setFont(font);
    //根据配置文件名初始化应用程序上下文。
    QString configFileName2(configFileName);
    if (configFileName2.isNull() || configFileName2.isEmpty())
    {
        configFileName2 = QCoreApplication::applicationFilePath();
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
        QChar fileExtStr[] = {'.', 'c', 'o', 'n', 'f', 'i', 'g'};
        configFileName2.append(fileExtStr, 7);
#else
        char fileExtStr[] = {'.', 'c', 'o', 'n', 'f', 'i', 'g', '\0'};
        configFileName2.append(fileExtStr);
#endif
    }
    WRAppContext::current()->initialize(configFileName2);
}
//清理当前对象所占用的相关资源。
WRWorkGuiApplication::~WRWorkGuiApplication()
{
    //add code here
}

//删除窗口对象。
void WRWorkGuiApplication::onDeleteWindow(void)
{
    m_window = 0;
}
//新连接激活实例。
void WRWorkGuiApplication::onNewConnection(void)
{
    QLocalSocket* socket = m_server->nextPendingConnection();
    if (socket)
    {
        socket->deleteLater();
    }
    if (m_window)
    {
        m_window->activateWindow();
    }
}

//运行程序。
int WRWorkGuiApplication::exec(const WRWorkGuiConfig& config)
{
    //初始化工作程序。
    WRObjRef<WRWorkDaemon> daemon = WRObjRef<WRWorkDaemon>::create();
    //初始主窗口
    m_window = new WRWorkMainWindow(config, daemon);
    QObject::connect(m_window, SIGNAL(destroyed(QObject*)), this, SLOT(onDeleteWindow()));
    m_window->setAttribute(Qt::WA_DeleteOnClose, true);
    //显示主窗体
    m_window->show();
    //执行消息循环
    return m_app.exec();
}
//检测是否有实例正在运行,如果正在运行，则返回True，否则返回False。
bool WRWorkGuiApplication::check(const QString& serverName)
{
    QLocalSocket socket;
    socket.connectToServer(serverName);
    if (socket.waitForConnected(50))
    {
        return true;
    }
    return false;
}

//运行程序。
int WRWorkGuiApplication::run()
{
    //界面配置
    WRWorkGuiConfig config;
    if (!config.init())
    {
        QMessageBox::critical(0, QStringLiteral("应用程序错误"),
                              QStringLiteral("配置异常，可能是配置文件不存在或者配置格式错误！"),
                              QMessageBox::Ok, QMessageBox::Ok);
        return 0;
    }
    //执行程序
    QString mutex = config.mutex();
    if (mutex.isNull() || mutex.isEmpty())
    {
        return exec(config);
    }
    else
    {
        if (!check(mutex))
        {
            QLocalServer::removeServer(mutex);
            if (m_server->listen(mutex))
            {
                QObject::connect(m_server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
                return exec(config);
            }
        }
        return 0;
    }
}
